package com.zhku.zhny.admin.web.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import com.alibaba.fastjson.JSON;
import com.zhku.zhny.admin.constant.ConstantFilePath;
import com.zhku.zhny.admin.exception.RemoveException;
import com.zhku.zhny.admin.exception.SearchException;
import com.zhku.zhny.admin.service.AdminService;
import com.zhku.zhny.admin.service.AdminServiceImpl;
import com.zhku.zhny.domain.Event;
import com.zhku.zhny.domain.EventPageBean;
import com.zhku.zhny.util.BaseServlet;
import com.zhku.zhny.util.FileOperationUtil;

@WebServlet("/info/event")
public class EventInfoServlet extends BaseServlet{

	private static final long serialVersionUID = 1L;
	/**
	 * 添加事记信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addEvent(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException{
		Map<String, String[]> map = null;
		try {
			map = FileOperationUtil.upload(request, ConstantFilePath.LOCALEVENTFILEPATH);
			Event event = new Event();
			ConvertUtils.register(new DateLocaleConverter(),Date.class);// 将字符串日期转换为日期类型
			BeanUtils.populate(event, map);
			AdminService adminService = new AdminServiceImpl();
			int i = adminService.addEventInfo(event);
			if (i > 0) {
				return "redirect:/admin/success.html";
			}else {
				return "redirect:/admin/fail.html";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:/admin/fail.html";
		}
	}
	
	/**
	 * 查询出所有事记信息，前台接口
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void searchEvents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AdminService adminService = new AdminServiceImpl();
		try {
			List<Event>events = adminService.findAllEvent();
			Event event = null;
			for (int i = 0; i < events.size(); i++) {
				event = events.get(i);
				// 修改小组图片访问路径
				String newPath = ConstantFilePath.EVENTDIRECTORY + event.getEventImgPath();
				event.setEventImgPath(newPath);
			}
			String data = JSON.toJSONString(events);
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(data);
		} catch (SearchException e) {
			e.printStackTrace();
			response.getWriter().print("");
		}
	}

	/**
	 * 删除事记信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void removeEvents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String param = request.getParameter("ids");
		String[] ids = param.split(",");
		AdminService adminService = new AdminServiceImpl();
		boolean result = false;
		try {
			result = adminService.removeEventByIds(ids);
			if(result) {
				response.getWriter().print(result);
			}else {
				response.getWriter().print(result);
			}
		} catch (RemoveException e) {
			e.printStackTrace();
			response.getWriter().print(result);
		}
	}
	
	/**
	 * 分页查询事记信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void fySearchEvents(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String title = request.getParameter("title");
		String date = request.getParameter("date");
		String currentPage = request.getParameter("currentPage");
		int pageSize = 10;// 设置每页显示记录数
		//调用业务逻辑
		AdminService adminService = new AdminServiceImpl();
		EventPageBean pageBean = new EventPageBean();
		try {
			pageBean = adminService.findByTitleOrDate(title, date, currentPage, pageSize);
			String data = JSON.toJSONString(pageBean);// 将数据封装为json对象
			// 响应回客户端
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(data);
		} catch (SearchException e) {
			e.printStackTrace();
			response.getWriter().print("");
		}
	}
}
